//
//  DHTabViewController.m
//  DeepBreathing
//
//  Created by DreamHack on 15-12-15.
//  Copyright (c) 2015年 DreamHack. All rights reserved.
//

#import "DHTabViewController.h"
#import "DHDrawerViewController.h"

@interface DHTabViewController ()

@property (nonatomic, strong) NSArray * viewControllers;
@property (nonatomic, strong) NSArray * normalStateImages;
@property (nonatomic, strong) NSArray * selectedImages;

@property (nonatomic, strong) UIView * viewContainer;
@property (nonatomic, strong) UIView * buttonContainer;

- (void)initializeAppearance;

@end

enum {
    ButtonTag = 100,
};

@implementation DHTabViewController

#pragma mark - initialize
// designate
- (instancetype)initWithViewControllers:(NSArray *)viewControllers normalStateImages:(NSArray *)normalImages selectedStateImages:(NSArray *)selectedImages
{
    self = [super init];
    if (self) {
        self.viewControllers = viewControllers;
        self.normalStateImages = normalImages;
        self.selectedImages = selectedImages;
    }
    return self;
}

- (instancetype)initWithViewControllers:(NSArray *)viewControllers
{
    return [self initWithViewControllers:viewControllers normalStateImages:nil selectedStateImages:nil];
}
#pragma mark - life cycle
//
//- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    DHDrawerViewController * vc = (DHDrawerViewController *)self.parentViewController;
//    [vc closeLeftDrawer];
//}
//
//- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    DHDrawerViewController * vc = (DHDrawerViewController *)self.parentViewController;
//    [vc openLeftDrawer];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeAppearance];
}
#pragma mark - override
#pragma mark - private methods
- (void)initializeAppearance
{
    [self.view addSubview:self.viewContainer];
    [self.view addSubview:self.buttonContainer];
    [self.viewControllers enumerateObjectsUsingBlock:^(UIViewController * vc, NSUInteger idx, BOOL *stop) {
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage * normalImage = self.normalStateImages[idx];
        UIImage * selectedImage = self.selectedImages[idx];
        [button setImage:normalImage forState:UIControlStateNormal];
        [button setImage:selectedImage forState:UIControlStateSelected];
        CGFloat width = 375.f/self.viewControllers.count;
        CGFloat height = width/(normalImage.size.width/normalImage.size.height);
        button.frame = CGRectMake(width*idx, 0, width, height);
        [button addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = ButtonTag+idx;
        [self.buttonContainer addSubview:button];
    }];
    self.selectedIndex = 0;
}
#pragma mark - interface methods
#pragma mark - callback/action

- (void)respondsToButton:(UIButton *)sender
{
    self.selectedIndex = sender.tag - ButtonTag;
}
#pragma mark - system protocol
#pragma mark - custom protocol
#pragma mark - setter
- (void)setSelectedIndex:(NSInteger)selectedIndex
{
    
    // 移除当前的controller
    UIViewController * currentViewController = self.viewControllers[_selectedIndex];
    if (currentViewController.parentViewController) {
        [currentViewController.view removeFromSuperview];
        [currentViewController removeFromParentViewController];
        [currentViewController willMoveToParentViewController:nil];
        
    }
    // 把之前选中的按钮的selected设为NO
    UIButton * currentButton = (UIButton *)[self.buttonContainer viewWithTag:ButtonTag+_selectedIndex];
    currentButton.selected = NO;
    
    _selectedIndex = selectedIndex;
    UIViewController * controller = self.viewControllers[selectedIndex];
    // 关联controller
    [self addChildViewController:controller];
    // 添加视图层级
    [self.viewContainer addSubview:controller.view];
    [controller didMoveToParentViewController:self];
    // 把即将选中的按钮的selected设为YES
    UIButton * button = (UIButton *)[self.buttonContainer viewWithTag:ButtonTag + selectedIndex];
    button.selected = YES;
}

#pragma mark - getter
- (UIView *)viewContainer
{
    if (!_viewContainer) {
        _viewContainer = [[UIView alloc] initWithFrame:self.view.bounds];
    }
    return _viewContainer;
}


- (UIView *)buttonContainer
{
    if (!_buttonContainer) {
        
        CGFloat height = 375.f/5/1.28;
        
        _buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 667-height, 375.f, height)];
    }
    return _buttonContainer;
}









@end
